#include "PlaylistView.h"
#include <QMenu>
#include "ApplicationIcon.h"

// media engine resources
#include "AbstractMediaSource.h"
#include "MediaListModel.h"
#include <MediaEngine.h>


PlaylistView::PlaylistView( ActionListController & actionController,
                            ActionListPainter * painter,
                            QWidget * parent) :
   ActionListView( actionController, painter, parent),
   m_contextMenu( new QMenu(parent)),
   m_playOverlayAction( new QAction( QIcon(IconPath("track_play.png")),
                                     tr("play in overlay"), this)),
   m_pauseOverlayAction( new QAction( QIcon(IconPath("track_pause.png")),
                                      tr("pause in overlay"), this)),
   m_overlayEngine( new MediaEngine(this))
{
   connect( this, SIGNAL(customContextMenuRequested(QPoint)),
            this, SLOT(onContextMenuRequested(QPoint)) );

   m_contextMenu->insertActions( nullptr, QList<QAction *>()
                                 << m_playOverlayAction
                                 << m_pauseOverlayAction);
}

PlaylistView::~PlaylistView()
{
   delete m_playOverlayAction;
   delete m_contextMenu;
}



void PlaylistView::onContextMenuRequested(const QPoint & location)
{
   QModelIndex index = indexAt( location);

   if (index.isValid())
   {
      QAction * selected = m_contextMenu->exec(this->mapToGlobal(location));

      // It is not really good that this module holds the media engine and operates
      // on it, but, so far, the engine is used in a minimal way and it's better not
      // to place relevant code elsewhere.
      if (selected == m_playOverlayAction)
      {
         const AbstractMediaSource *source = dynamic_cast<MediaListModel *>(model())->mediaData( index);

         m_overlayEngine->setCurrentSource( source);
         m_overlayEngine->play();
      }
      else if (selected == m_pauseOverlayAction)
      {
         m_overlayEngine->stop();
      }
   }
}

